package com.cskaoyan.aspect.wx_user;

import com.cskaoyan.bean.temporary.TemporaryUser;
import com.cskaoyan.bean.user.Footprint;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.bean.wx_footprint.base.FootprintParam;

import com.cskaoyan.bean.wx_goods.WxGoodsDetailVO;
import com.cskaoyan.mapper.temporary.TemporaryUserMapper;
import com.cskaoyan.mapper.user.FootprintMapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author liXiaoFei
 * @date 2021/10/19 17:35
 */

@Aspect
@Component
public class WxFootprintAspect {

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    TemporaryUserMapper temporaryUserMapper;

    @Pointcut("execution(* com.cskaoyan.service.wx_footprint..queryFootprintListByUser(..))")
    public void footprintListQueryPointcut(){
    }

    @Pointcut("execution(* com.cskaoyan.service.wx_goods.WxGoodsServiceImpl.goodsDetail(..))")
    public void footprintDetailPointcut(){
    }

    @Around("footprintListQueryPointcut()")
    public Object aroundFootprintListQuery(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        FootprintParam footprintParam = (FootprintParam) args[0];
        PageHelper.startPage(footprintParam.getPage(),footprintParam.getLimit());
        return joinPoint.proceed();
    }

    @AfterReturning(value = "footprintDetailPointcut()",returning = "wxGoodsDetailVO")
    public void afterFootprintDetail(JoinPoint joinPoint,WxGoodsDetailVO wxGoodsDetailVO) throws Throwable {
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        if (principals == null){
            TemporaryUser temporaryUser = new TemporaryUser();
            temporaryUser.setAddTime(new Date(System.currentTimeMillis()));
            temporaryUser.setGoodsId(wxGoodsDetailVO.getInfo().getId());
            Object[] args = joinPoint.getArgs();
            HttpServletRequest httpServletRequest = (HttpServletRequest) args[1];
            String addr = httpServletRequest.getRemoteAddr();
            temporaryUser.setUserIp(addr);
            temporaryUserMapper.insertSelective(temporaryUser);
        }else {
            Footprint footprint = new Footprint();
            footprint.setGoodsId(wxGoodsDetailVO.getInfo().getId());
            footprint.setAddTime(new Date(System.currentTimeMillis()));
            footprint.setUpdateTime(new Date(System.currentTimeMillis()));
            footprint.setDeleted(false);
            User user = (User) principals.getPrimaryPrincipal();
            footprint.setUserId(user.getId());
            footprintMapper.insert(footprint);
        }
    }




}
